package com.east.service.system.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.east.bean.Role;
import com.east.bean.User;
import com.east.bean.UserRole;
import com.east.dao.RoleMapper;
import com.east.dao.UserMapper;
import com.east.dao.UserRoleMapper;
import com.east.service.system.UserService;
import com.east.utils.Pagenation;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service("userService")
public class UserServiceImpl implements UserService{
	@Autowired
	UserMapper userMapper;
	@Autowired
	UserRoleMapper userRoleMapper;
	@Autowired
	RoleMapper roleMapper;
	@Override
	public List<User> findByRole(String account, String pwd) {
		List<Integer> finByRoleId = userMapper.finByRoleId(account, pwd);
		System.out.println(finByRoleId);
		return null;
	}
	@Override
	public Page<User> findUserAll(Pagenation pagenation, String key) {
		Page<User> startPage = PageHelper.startPage(pagenation.getPageNum(), pagenation.getPageSize());	
		HashMap<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("key", key);
		userMapper.findByAll(hashMap);
		return startPage;
	}
	@Override
	public User findByUserId(User user) {
		List<Role> selectAll = roleMapper.selectAll();
		User findByUserId = userMapper.findByUserId(user.getuId());
		for (Role role : selectAll) {
			for (Role r : findByUserId.getRoles()) {
				if(r.getId().equals(role.getId())) {
					role.setChecked(true);
				}
			}
		}
		findByUserId.setRoles(selectAll);
		return findByUserId;
	}
	@Transactional
	@Override
	public int updateUser(User user, String[] userRole) {
		//删除角色
		if(userRole!=null) {
			userRoleMapper.delUserRoleId(user.getuId());
			List<UserRole> list=new ArrayList<UserRole>();
			for (String string : userRole) {
				UserRole us = new UserRole();
				us.setRoleId(Integer.valueOf(string));
				us.setUserId(user.getuId());
				list.add(us);
			}
			userRoleMapper.insertUserRole(list);
		}
	
		//修改用户账号和密码
		User selectByPrimaryKey = userMapper.selectByPrimaryKey(user.getuId());
		selectByPrimaryKey.setuAccount(user.getuAccount());
		selectByPrimaryKey.setuPhone(user.getuPhone());
		//selectByPrimaryKey.setuSalt(user.getuSalt());
		selectByPrimaryKey.setuState(user.getuState());
		selectByPrimaryKey.setuPassword(user.getuPassword());
		selectByPrimaryKey.setuLoginTime(new Date());
		return userMapper.updateByPrimaryKey(selectByPrimaryKey);
	}
	@Override
	public boolean findByAccoount(User user) {
		Integer findByAccount = userMapper.findByAccount(user.getuAccount());
		if(findByAccount<1) {
			return false;
		}else {
			return true;
		}
		
	}
	@Override
	public boolean findByAcount(String account) {
		Example example = new Example(User.class);
		Criteria cr = example.createCriteria();
		cr.andEqualTo("uAccount", account);
		List<User> selectByExample = userMapper.selectByExample(example);
		if(selectByExample.size()>0) {
			return true;
		}else {
			return false;
		}
		
		
	}
	@Override
	public int updateUser(User user) {
		User users = userMapper.selectByPrimaryKey(user.getuId());
		users.setuPassword(user.getuPassword());
		int updateByPrimaryKey = userMapper.updateByPrimaryKey(users);
		return updateByPrimaryKey;
	}
}
